#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll N = 1e3 + 10;
ll g[N][N], ans[N][N], vis[N][N], cnt = 0, n;
ll dx[] = {0, 1, 0, -1};
ll dy[] = {1, 0, -1, 0};
ll nx = 0;
void dfs(ll x, ll y, ll cnt, ll nx)
{
    vis[x][y] = 1;
    if (cnt == n * n)
        return;
    ans[x][y] = cnt;
    dfs(x + dx[nx % 4], y + dy[nx % 4], cnt + 1, nx + 1);
}
void solve()
{
    memset(g, 0, sizeof g);
    memset(ans, 0, sizeof ans);
    memset(vis, 0, sizeof vis);
    cnt = 0;
    nx = 0;
    cin >> n;
    if (n % 2 == 1)
    {
        ll stx = (n + 1) / 2, sty = (n + 1) / 2;
        dfs(stx, sty, 0, 0);
    }
    else
    {
        ll stx = (n) / 2, sty = (n) / 2;
        dfs(stx, sty, 0, 0);
    }

    for (ll i = 1; i <= n; i++)
    {
        for (ll j = 1; j <= n; j++)
        {
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }
    return;
}
int main()
{
    ll t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}